﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace xq引擎
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void Button_Search_Click(object sender, EventArgs e)
        {
            try
            {
                string fen = this.textBox_Fen.Text.Trim();
                int timeSpan = int.Parse(this.textBox_ThinkTime.Text.Trim());
                string result = BestStep(fen, timeSpan);
                this.richTextBox_Result.AppendText(result + "\r\n\r\n");
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public static string BestStep(string fen,int thinkTime)
        {
            Process proEngine = new Process();

            proEngine.StartInfo.FileName = "eleeye.exe";

            proEngine.StartInfo.CreateNoWindow = true;
            proEngine.StartInfo.RedirectStandardError = true;
            proEngine.StartInfo.RedirectStandardInput = true;
            proEngine.StartInfo.RedirectStandardOutput = true;
            proEngine.StartInfo.UseShellExecute = false;

            proEngine.Start();

            proEngine.StandardInput.WriteLine("ucci");
            proEngine.StandardInput.WriteLine("setoption batch on");
            proEngine.StandardInput.WriteLine("position fen " + fen);
            proEngine.StandardInput.WriteLine("go time " + thinkTime.ToString());
            proEngine.StandardInput.WriteLine("quit");
            string output = proEngine.StandardOutput.ReadToEnd();
            proEngine.Close();

            return output;

            //int start = output.IndexOf("bestmove");

            //return output.Substring(start);
        }
    }
}
